package gui;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import gui.exception.ActivityNotFoundException;
import gui.framework.Loader;
import gui.framework.intent.Intent;
import gui.parser.ManifestParser;
import gui.parser.bean.Manifest;

/**
 * 布局文件加载器
 * 
 * @author chenj_000
 *
 */
public final class GUILoader implements Loader {
	private Manifest manifest;

	@Override
	public void loadManifest() {
		manifest = new ManifestParser()
				.parser(new InputStreamReader(Context.getResource().getConfiguration("Manifest.xml")));
	}

	@Override
	public Application getApplication() {
		return manifest.getApplication();
	}

	@Override
	public List<Activity> getActivities() {
		return manifest.getActivity_list();
	}

	@Override
	public List<Activity> getLuncherActivities() {
		List<Activity> a_l = new ArrayList<Activity>();
		for (Activity a : getActivities()) {
			if (a.getIntentFilter().isIntent(Intent.action.MAIN, null)) {
				a_l.add(a);
			}
		}
		return a_l;
	}

	@Override
	public Activity getActivity(Class<? extends Activity> activity) {
		for (Activity a : getActivities()) {
			if (a.getClass().equals(activity)) {
				return a;
			}
		}
		throw new ActivityNotFoundException();
	}

	@Override
	public Activity getActivity(Intent intent) {
		for (Activity a : getActivities()) {
			if (a.getIntentFilter() != null && a.getIntentFilter().isIntent(intent)) {
				return a;
			}
		}
		throw new ActivityNotFoundException();
	}

}
